package com.by;

import com.rabbitmq.client.*;
import com.rabbitmq.client.impl.AMQBasicProperties;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1.创建连接工厂   创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("erp");//虚拟主机 默认是 /
        factory.setPort(5672); //连接端口号
        factory.setHost("192.168.203.130");//服务器地址 默认是localhost
        factory.setUsername("admin");
        factory.setPassword("123456");

        //2.创建连接  资源自动释放（连接、信道 关闭）
        try (Connection conn = factory.newConnection(); Channel channel = conn.createChannel()) {


            //3.声明队列
            String queueName = "msg-ttl";
            Map<String, Object> arg = new HashMap<>();
//            arg.put("x-message-ttl", 6000);//6s 消息的 生命周期
            arg.put("x-expires", 10000);//设置队列的TTl
            channel.queueDeclare(queueName, true, false, false, arg);

            //4.发布消息
            /**
             * MessageProperties.PERSISTENT_TEXT_PLAIN消息持久化
             */
            AMQP.BasicProperties properties = new AMQP.BasicProperties()
                    .builder()
                    .expiration("10000")//设置消息过期时间 单位是s
                    .build();
            channel.basicPublish("", queueName, null, "消息过期".getBytes());


            System.out.println("消息发布完毕，服务器已收到");
        }

    }
}